package com.common.tools.controller;

import com.common.convert.ConvertException;
import com.common.convert.ConverterConfig;
import com.common.convert.ConverterRouter;
import com.common.tools.utils.MultipartFileUtils;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.document.DocumentFormat;
import org.jodconverter.core.document.DocumentFormatRegistry;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.filter.PagesSelectorFilter;
import org.jodconverter.local.office.LocalOfficeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("convert")
public class ConvertController {

    @Autowired
    private DocumentConverter documentConverter;
    private ConverterRouter converterRouter;


    @Value("${converter.workDir}")
    private String workDir;

    @Value("${test.sourceFile}")
    private String sourceFile;

    @Value("${test.targetFile}")
    private String targetFile;


    @Autowired
    OfficeManager localOfficeManager;

    @Autowired
    DocumentFormatRegistry documentFormatRegistry;

    @Autowired
    public ConvertController() {
        this.converterRouter = new ConverterRouter();
    }

    @PostConstruct
    public void init() {
        ConverterConfig.DOCUMENT_CONVERTER = documentConverter;
        ConverterConfig.OUT_DIR = workDir + File.separator + "out" + File.separator;
    }

    @PostMapping("office")
    public String office(@RequestParam("file") MultipartFile file, String outType) throws IOException, OfficeException {
        File tempFile = MultipartFileUtils.save(file, workDir);
        return converterRouter.convert(tempFile, outType);
    }

    // 安装 LibreOffice 就可以用了。
    // 下载地址：https://www.libreoffice.org/download/download-libreoffice/?type=rpm-x86_64&version=7.5.5&lang=zh-CN
    @GetMapping("/test")
    public String test() {


        DocumentFormat doc = DefaultDocumentFormatRegistry.getFormatByExtension("doc");
        DocumentFormat png = DefaultDocumentFormatRegistry.PNG;

        try {
            File inputFile = new File(sourceFile);
            File outFile = new File(targetFile);

            // word转图片，这个只是转了第一页
            documentConverter.convert(inputFile).to(outFile).execute();
            documentConverter.convert(inputFile).as(doc).to(new File("d:/2-test2.png")).as(png).execute();

            // word转pdf，是转了所有页
            documentConverter.convert(inputFile).as(doc).to(new File("d:/2-test2.pdf")).as(DefaultDocumentFormatRegistry.PDF).execute();


            // word转图片，这个可以指定页
            //convertPage(2);

        } catch (Exception e) {
            throw new ConvertException(e);
        }

        return "ok";
    }

    public void convertPage(int page) throws OfficeException {
        Map<String, Object> loadProperties = new HashMap();
        loadProperties.putAll(LocalConverter.DEFAULT_LOAD_PROPERTIES);
        loadProperties.put("UpdateDocMode", Short.valueOf((short) 1));

        PagesSelectorFilter fio = new PagesSelectorFilter( 1,2,3 );
        LocalConverter converter =  LocalConverter.builder()
                .filterChain(fio)
                .officeManager(localOfficeManager)
                .formatRegistry(documentFormatRegistry)
                .loadDocumentMode(LocalConverter.DEFAULT_LOAD_DOCUMENT_MODE)
                .loadProperties(loadProperties).build();

        File inputFile = new File(sourceFile);
        File outFile = new File(targetFile);
        converter.convert(inputFile).to(outFile).execute();
    }

}
